package cz.gelion.shylock.invoice.test

import javax.persistence.Persistence
import cz.gelion.shylock.invoice.entity.Subject
import cz.gelion.shylock.invoice.entity.Subject
import cz.gelion.shylock.invoice.entity.Manager
import cz.gelion.shylock.invoice.entity.Account
import cz.gelion.shylock.invoice.entity.SubjectUnit
import java.util.List
import java.util.ArrayList
import cz.gelion.shylock.invoice.entity.SubjectUnitAccount
import cz.gelion.shylock.invoice.CurrentEnv
import org.apache.log4j.BasicConfigurator
import org.apache.log4j.Logger
import org.apache.log4j.Level
import cz.gelion.shylock.invoice.entity.InvoiceManager

object Init {

  
	  var emf = Persistence.createEntityManagerFactory("shylock")
		
	  
	  
	  
  
  def main(args: Array[String]) {
	    
	    BasicConfigurator.configure()
	    Logger.getLogger("cz.gelion.shylock").setLevel(Level.TRACE)
	    Logger.getLogger("org.hibernate").setLevel(Level.ERROR)
	    
	    println(emf);
	    
	    CurrentEnv._manager.value = new Manager("ja");
	    
	    val manager = new Manager("ja")
	    
	    var em = emf.createEntityManager();
	    
	    em.getTransaction().begin()
	    
	    em.createQuery("delete from SubjectUnitAccount s").executeUpdate();
	    em.createQuery("delete from SubjectUnit s").executeUpdate();
	    em.createQuery("delete from Subject s").executeUpdate();
	    em.createQuery("delete from Account s").executeUpdate();
	    
	    em.getTransaction().commit()
	    
	    
	    
	    em.getTransaction().begin()
	    val subj = new Subject()
	    subj.setName("Hora�ek")
	    subj.setGivenname("Jan Anton�n")
	    subj.setPrefix("prof.");
	    em.persist(subj)
	    em.getTransaction().commit()
	    
	    println(subj.getId())
	    
	    
	    var i = em.createQuery("select s from Subject s", classOf[Subject]).getResultList().iterator()
	    println(i)
	    
	    
	    em.getTransaction().begin()
	    val accList = new ArrayList[Account]()
	    for (i <- 1 to 3) {
	    
	    	val acc = new Account()
	    	acc.setManager(manager)
	    	acc.setSubject(subj)
	    	acc.setAccountNo(i + "-12345/123")
	    	em.persist(acc)
	    	accList.add(acc)
	    }
	    em.getTransaction.commit
	    
	    
	    
	    em.getTransaction.begin
	    var unitList = new ArrayList[SubjectUnit]
	    for (i <- 1 to 2) {
	      val unit = new SubjectUnit
	      unit.subject = subj
	      unit.setName("Unit " + i)
	      unitList.add(unit)
	      em.persist(unit)
	    }
	    
	    em.getTransaction().commit()
	    
	    
	    em.getTransaction.begin
	    
	    var sua = new SubjectUnitAccount(accList.get(0), unitList.get(0))
	    sua.note = "pozn�mka"
	    em.persist(sua)
	    em.getTransaction.commit
	    
	    em.getTransaction.begin
	    var invoiceManager = new InvoiceManager(new InvoiceManager.PK(manager,unitList.get(1)))
	    em.persist(invoiceManager)
	    em.getTransaction.commit
  }
  
	  
	  
	  
  
}